package day23;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Homework3 {
    public static void main(String[] args) {
        File in = new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\kejian.mp4");
        File out = new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\bb\\my.mp4");
        if (in.exists()) {
            File pOut = out.getParentFile();
            if (!pOut.exists()) pOut.mkdirs();
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(in);
                fos = new FileOutputStream(out);
                byte[] bs = new byte[1024];
                int len;
                int lastProgress=-1;
                long total = 0;
                long fileSize = in.length();
                while ((len = fis.read(bs)) != -1) {
                    fos.write(bs, 0, len);
                    total += len;
                    int progress = (int) ((total * 100) / fileSize);
                    if (progress % 10 == 0&&progress!=lastProgress) { // 每10%输出一次进度
                        System.out.println("当前进度: " + progress + "%");
                        lastProgress=progress;
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null) fis.close();
                    if (fos != null) fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            System.out.println("文件不存在！");
        }
    }
}
